package com.atguigu.jxc.controller;

import com.atguigu.jxc.domain.ServiceVO;
import com.atguigu.jxc.entity.Supplier;
import com.atguigu.jxc.service.SupplierService;
import com.google.gson.Gson;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @author luojia
 * @create 2022-01-01 23:05
 */
@RestController
@RequestMapping("supplier")
public class SupplierController {

    @Autowired
    private SupplierService supplierService;

    /**
     * 分页查询供应商
     *
     * @param page
     * @param rows
     * @param supplierName
     * @return
     */
    @PostMapping("list")
    public String querySupplier(@RequestParam("page") Integer page,
                                @RequestParam("rows") Integer rows,
                                @RequestParam(value = "supplierName", required = false) String supplierName) {

        String supplierList = supplierService.querySupplier(page, rows, supplierName);

        return supplierList;
    }

    /**
     * 供应商添加或修改
     *
     * @param supplier
     * @param supplierId
     * @return
     */
    @PostMapping("save")
    public String supplierAddOrUpdate(Supplier supplier, @RequestParam(value = "supplierId", required = false) Integer supplierId) {
        supplierService.supplierAddOrUpdate(supplier, supplierId);
        Gson gson = new Gson();
        return gson.toJson(new ServiceVO(100, "请求成功", null));
    }

    /**
     * 删除供应商（支持批量删除）
     *
     * @param ids
     * @return
     */
    @PostMapping("delete")
    public String supplierDelete(String ids) {
        String[] split = ids.split(",");
        supplierService.supplierDelete(split);

        Gson gson = new Gson();
        return gson.toJson(new ServiceVO(100, "请求成功", null));
    }
}
